package org.csu.backofficemanagersys.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.csu.backofficemanagersys.Response.ResponseMsg;
import org.csu.backofficemanagersys.domain.Order;
import org.csu.backofficemanagersys.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/mypetstore/orders")
@Log4j2
public class OrderController {
    private OrderService orderService;

    @Autowired
    public void setOrderDao (OrderService orderService) {
        this.orderService =orderService;
    }

    //TODO 更新
    @PutMapping("/table")
    public void orderUpdate(@ModelAttribute Order order){
        orderService.updateOrder(order);
    }

    //TODO 通过username查找
    @GetMapping("/table/{username}")
    @ResponseBody
    public ResponseMsg<Order> orderSelectByName(@PathVariable String username){
        return ResponseMsg.createForSuccess(orderService.selectOneByName(username));
    }

    //TODO 删除
    @DeleteMapping("/table/{id}")
    public ResponseMsg<Object> orderDelete(@PathVariable int id){
        if(orderService.delectAdminById(id)){
            return ResponseMsg.createForSuccess();
        }
        return ResponseMsg.createForError();
    }

    //TODO 发货
    @GetMapping("/delivery/{id}")
    public ResponseMsg<Object> orderDelivery(@PathVariable int id){
        if(orderService.delivery(id)){
            return ResponseMsg.createForSuccess();
        }
        return ResponseMsg.createForError();
    }


    //TODO 查找所有未发货订单
    @GetMapping("/index/{delivery}")
    @ResponseBody
    public ResponseMsg<JSONObject> orderSelectNoDelivery(int currentPage, int size,@PathVariable int delivery){
        try {
            List<Order> orderList = orderService.Delivery(delivery);
            JSONObject jsonObject = new JSONObject();
            List<Order> orders;
            if(orderList.size()<size*currentPage && orderList.size()>(currentPage-1)*size){
                orders = orderList.subList(size * (currentPage - 1), orderList.size());
            }else {
                orders = orderList.subList(size * (currentPage - 1), size * currentPage - 1);
            }
            jsonObject.put("total",orderList.size());
            jsonObject.put("list",orders);

            return ResponseMsg.createForSuccess("获取表格元素成功",jsonObject);
        }catch (Exception e){
            return ResponseMsg.createForError("获取表格元素失败");
        }
    }

    @GetMapping("/table")
    public ResponseMsg<JSONObject> find(int currentPage,int size,String username,String id){
        List<Order> orderList;
        if(username == null && id == null){
           return TableA(currentPage,size);
        } else if (username !=null && id != null) {
            orderList = orderService.findOrderLike(username,Integer.parseInt(id));
        } else if (username ==null) {
            orderList = orderService.findOrderLike(Integer.parseInt(id));
        }else {
            orderList = orderService.findOrderLike(username);
        }
        if(orderList.size() == 0){
            return ResponseMsg.createForSuccess("表格为空",null);
        }
        JSONObject jsonObject = new JSONObject();
        List<Order> orders;
        try {
            if(orderList.size()<size*currentPage && orderList.size()>(currentPage-1)*size){
                orders = orderList.subList(size * (currentPage - 1), orderList.size());
            }else {
                orders = orderList.subList(size * (currentPage - 1), size * currentPage - 1);
            }
            jsonObject.put("total",orderList.size());
            jsonObject.put("list",orders);

            return ResponseMsg.createForSuccess("获取表格元素成功",jsonObject);
        }catch (Exception e){
            return ResponseMsg.createForError("获取表格元素失败");
        }
    }



    //TODO 查找所有订单
    @GetMapping("/index")
    @ResponseBody
    public ResponseMsg<List<Order>> orderSelectAll(){
        return ResponseMsg.createForSuccess(orderService.orderList());
    }

    public ResponseMsg<JSONObject> TableA(int currentPage, int size){
        try {
            List<Order> orderList = orderService.orderList();
            if (orderList == null){
                return ResponseMsg.createForSuccessMsg("表格为空");
            }
            JSONObject jsonObject = new JSONObject();
            List<Order> orders;
            if(orderList.size()<size*currentPage && orderList.size()>(currentPage-1)*size){
                orders = orderList.subList(size * (currentPage - 1), orderList.size());
            }else {
                orders = orderList.subList(size * (currentPage - 1), size * currentPage - 1);
            }
            jsonObject.put("total",orderList.size());
            jsonObject.put("list",orders);

            return ResponseMsg.createForSuccess("获取表格元素成功",jsonObject);
        }catch (Exception e){
            return ResponseMsg.createForError("获取表格元素失败");
        }
    }

}
